package com.seeyon.apps.kk.manager;

import java.rmi.RemoteException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.locknlock.gw.ifgw002.IFGW002_CN_GW_SOServiceStub;
import com.locknlock.gw.ifgw002.IFGW002_CN_GW_SOServiceStub.IFGW002_CN_GW_REQ_DT;
import com.locknlock.gw.ifgw002.IFGW002_CN_GW_SOServiceStub.IFGW002_CN_GW_REQ_MT;
import com.locknlock.gw.ifgw002.IFGW002_CN_GW_SOServiceStub.IFGW002_CN_GW_RES_MT;
import com.locknlock.gw.ifgw002.IFGW002_CN_GW_SOServiceStub.ITEM_type0;
import com.locknlock.gw.ifgw002.IFGW002_CN_GW_SOServiceStub.RETURN_type0;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.util.json.JSONUtil;
import com.seeyon.kk.utils.ValueUtils;
import com.seeyon.v3x.services.form.FormFactory;
import com.seeyon.v3x.services.form.FormUtils;
import com.seeyon.v3x.services.form.bean.FormExport;

public class LockOvertimeSapManager extends BaseManager {
	private static final Log log = LogFactory.getLog(LockOvertimeSapManager.class);
	private FreemarkFlowManager freemarkFlowManager;

	public FreemarkFlowManager getFreemarkFlowManager() {
		return freemarkFlowManager;
	}

	public void setFreemarkFlowManager(FreemarkFlowManager freemarkFlowManager) {
		this.freemarkFlowManager = freemarkFlowManager;
	}

	public void AddOvertimeForm(Long id) {
		String sql = this.freemarkFlowManager.fltTemplateIntoString("sql/ghr/jbd/FIND_ALL_JBD_DATA.ftl", null);
		List<Map<String, Object>> v4 = this.jdbcTemplate.queryForList(sql, id);
		for (Map<String, Object> map : v4) {
			try {
				this.AddOvertimeLog(map);
				Map<String, Object> resultMap = this.AddOvertimeForm(map);
				this.UpdateLog(resultMap);
			} catch (RemoteException | ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public Map<String, Object> AddOvertimeForm(Map<String, Object> map) throws RemoteException, ParseException {
		log.info(JSONUtil.toJSONString(map));
		if (!map.isEmpty()) {
			IFGW002_CN_GW_SOServiceStub stub = new IFGW002_CN_GW_SOServiceStub();
			HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
			auth.setUsername("IFPO01");
			auth.setPassword("Passpo1$");
			stub._getServiceClient().getOptions().setProperty(HTTPConstants.AUTHENTICATE, auth);

			IFGW002_CN_GW_REQ_MT iFGW002_CN_GW_REQ_MT0 = new IFGW002_CN_GW_REQ_MT();
			IFGW002_CN_GW_REQ_DT param = new IFGW002_CN_GW_REQ_DT();
			ITEM_type0[] sapV2 = new ITEM_type0[map.size()];
			int num = 0;
			ITEM_type0 ii = new ITEM_type0();
			ii.setSRC_TRX_ID(MapUtils.getString(map, "邮箱唯一编号"));// 邮箱ID
			ii.setEMPNO(MapUtils.getString(map, "G-HR职员编号"));// 员工编号
			ii.setSEQ(MapUtils.getString(map, "SEQ"));// SEQID
			ii.setTL_DATE(MapUtils.getString(map, "申请开始日期"));
			ii.setOT_TYPE(MapUtils.getString(map, "加班类型"));// 开始日加班类型
			ii.setREQ_BGN_TIME(MapUtils.getString(map, "加班开始时间"));
			ii.setREQ_END_TIME(MapUtils.getString(map, "加班结束时间"));
			ii.setPUNCH_BGN_TIME(MapUtils.getString(map, "考勤开始时间"));
			ii.setPUNCH_END_TIME(MapUtils.getString(map, "考勤结束时间"));
			ii.setREPLACE_DT("");// 调休日期
			ii.setREQ_REASON(MapUtils.getString(map, "事由"));
			ii.setGHR_STATUS("N");
			sapV2[num] = ii;
			num++;
			param.setITEM(sapV2);
			iFGW002_CN_GW_REQ_MT0.setIFGW002_CN_GW_REQ_MT(param);
			IFGW002_CN_GW_RES_MT ifgw002_CN_GW_SO = stub.IFGW002_CN_GW_SO(iFGW002_CN_GW_REQ_MT0);
			RETURN_type0[] as = ifgw002_CN_GW_SO.getIFGW002_CN_GW_RES_MT().getRETURN();
			map.put("XIAOXI", as[0].getMESSGAE());
			map.put("STATUS", as[0].getSTATUS());
			map.put("IF_DATE", as[0].getIF_DATE());
			log.info("加班申请单,返回结果：" + JSONUtil.toJSONString(as));
		}
		return map;
	}

	public Map<String, Object> AddOvertimeLog(Map<String, Object> vm) {
		// 日志表抓取数据
		vm.put("types", "加班申请单");// 推送类型
		String selectYsj = this.freemarkFlowManager.fltTemplateIntoString("sql/ghr/log/lock_jbdyuanshuju.ftl", vm);
		vm.put("source", selectYsj);// 推送源数据
		vm.put("agin", ValueUtils.getConfig().getLong("kk.CXCS"));// 是否重新传输
		vm.put("finsh", ValueUtils.getConfig().getLong("kk.WCPC"));// 完成抛送
		long mostSignificantBits = UUID.randomUUID().getMostSignificantBits();
		vm.put("SUIJI", mostSignificantBits);
		// 生成新的日志记录
		String dataXml = this.freemarkFlowManager.fltTemplateIntoString("sql/ghr/log/Lock_log.ftl", vm); // memberDay可以直接给
		// 可以使用数据模板生成，这样底表事件依然有效
		FormFactory formFactory = (FormFactory) AppContext.getBean("formFactory");
		try {
			FormExport[] formExportData = FormUtils.xmlTransformFormExports(dataXml);
			for (FormExport formExport : formExportData) {
				formFactory.importBusinessFormData("locknlockadmin", "Lock_log", formExport, null);
			}
		} catch (Exception e) {
			log.error(e);
		}
		return vm;
	}

	public void UpdateLog(Map<String, Object> map) {
		log.info("加班申请单，更新日志 start：" + JSONUtil.toJSONString(map));
		Map<String, Object> msp = new HashMap<>();
		msp.put("IFDATE", MapUtils.getString(map, "IF_DATE"));
		msp.put("STATUS", MapUtils.getString(map, "STATUS"));
		msp.put("MESSAGE", MapUtils.getString(map, "XIAOXI"));
		msp.put("SUI", MapUtils.getString(map, "SUIJI"));
		if (MapUtils.getString(map, "STATUS") == null || MapUtils.getString(map, "STATUS").equals("")) {
			msp.put("WPS", ValueUtils.getConfig().getLong("kk.LockLogWPS"));
			String updatesql = this.freemarkFlowManager.fltTemplateIntoString("sql/ghr/log/updatelogpssql.ftl", msp);
			this.jdbcTemplate.update(updatesql);
		} else {
			msp.put("YPS", ValueUtils.getConfig().getLong("kk.lockLogYPS"));
			String updatesql = this.freemarkFlowManager.fltTemplateIntoString("sql/ghr/log/updatelogsql.ftl", msp);
			this.jdbcTemplate.update(updatesql);
		}
		log.info("加班申请单，更新日志 end：" + JSONUtil.toJSONString(msp));
	}

}
